package com.song.shenghua.match.controller;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.song.shenghua.common.annotation.Log;
import com.song.shenghua.common.core.controller.BaseController;
import com.song.shenghua.common.core.domain.AjaxResult;
import com.song.shenghua.common.enums.BusinessType;
import com.song.shenghua.common.utils.poi.ExcelUtil;
import com.song.shenghua.match.domain.MatchHealth;
import com.song.shenghua.match.domain.MatchHealthMVo;
import com.song.shenghua.match.domain.MatchHealthVo;
import com.song.shenghua.match.domain.MatchHealthhourVo;
import com.song.shenghua.match.service.IMatchHealthService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 【请填写功能名称】Controller
 *
 * @author å®è¿å
 * @date 2021-03-07
 */
@RestController
@RequestMapping("/match/health")
public class MatchHealthController extends BaseController {
    @Autowired
    private IMatchHealthService matchHealthService;
    private MatchHealthVo matchHealthVo;

    /**
     * 查询【请填写功能名称】列表
     */
    @PostMapping("/list")
    public AjaxResult list(@RequestBody MatchHealth matchHealth) {
//        某些天的平均值
        DateTime offset = DateUtil.offsetDay(DateUtil.parse(DateUtil.format(new Date(), "yyyy-MM-dd")),  -7);

        matchHealth.setBeginTime(offset.toDateStr());
        String beginTime = matchHealth.getBeginTime();
        DecimalFormat df = new DecimalFormat("#0.0");
        List<MatchHealthVo> matchHealthVos = new ArrayList();
        for (int i = 0; i < 7; i++) {
            Date newDate = DateUtil.offset(DateUtil.parse(beginTime), DateField.DAY_OF_MONTH, i);
            matchHealth.setBeginTime(DateUtil.format(newDate,"yyyy-MM-dd"));
            Date newendDate = DateUtil.offset(DateUtil.parse(beginTime), DateField.DAY_OF_MONTH, i + 1);
            matchHealth.setEndTime(DateUtil.format(newendDate, "yyyy-MM-dd"));
            List<MatchHealth> matchHealthList = matchHealthService.selectMatchHealthList(matchHealth);
            if (matchHealthList.size() > 0) {
                double counttemp = 0.0;
                double counthumidity = 0.0;
                int countcough = 0;
                for (int j = 0; j < matchHealthList.size(); j ++) {
                    counttemp += matchHealthList.get(j).getTemperature();
                    counthumidity += matchHealthList.get(j).getHumidity();
                    countcough =countcough + matchHealthList.get(j).getCough();

                }
                matchHealthVo = new MatchHealthVo();
                matchHealthVo.setCountCough(countcough);
                matchHealthVo.setEquipmentId(matchHealthList.get(0).getEquipmentId());
                matchHealthVo.setUid(matchHealthList.get(0).getUid());
                matchHealthVo.setCountHumidity(Double.valueOf(df.format(counthumidity / matchHealthList.size())));
                matchHealthVo.setCountTemp(Double.valueOf(df.format(counttemp / matchHealthList.size())));
                matchHealthVo.setDate(DateUtil.parseDate(DateUtil.format(matchHealthList.get(0).getDatatime(), "yyyy-MM-dd")));
//            matchHealthVo.setMatchHealths(matchHealthList);
                matchHealthVos.add(matchHealthVo);
            }
        }
        return AjaxResult.success(matchHealthVos);
    }

    @PostMapping("/listhour")
    public AjaxResult listhour(@RequestBody MatchHealth matchHealth) {
//        每天12个小时的平均值
        DecimalFormat df = new DecimalFormat("#0.0");
        MatchHealthhourVo healthhourVo;
        String beginTime = matchHealth.getBeginTime();
        List<MatchHealthhourVo> matchHealthVos = new ArrayList();
        for (int i = 0; i < 24; i++) {
            Date newDate = DateUtil.offset(DateUtil.parse(beginTime), DateField.HOUR_OF_DAY, i);
            matchHealth.setBeginTime(DateUtil.format(newDate, "yyyy-MM-dd HH"));
            Date newendDate = DateUtil.offset(DateUtil.parse(beginTime), DateField.HOUR_OF_DAY, i + 1);
            matchHealth.setEndTime(DateUtil.format(newendDate, "yyyy-MM-dd HH"));
            List<MatchHealth> matchHealthList = matchHealthService.selectMatchHealthList(matchHealth);
            if (matchHealthList.size() > 0) {
                double counttemp = 0.0;
                double counthumidity = 0.0;
                int countcough = 0;
                for (int j = 0; j < matchHealthList.size(); j++) {
                    counttemp += matchHealthList.get(j).getTemperature();
                    counthumidity += matchHealthList.get(j).getHumidity();
                    countcough += matchHealthList.get(j).getCough();
                }
                healthhourVo = new MatchHealthhourVo();
                healthhourVo.setCountCough(countcough);
                healthhourVo.setEquipmentId(matchHealthList.get(0).getEquipmentId());
                healthhourVo.setUid(matchHealthList.get(0).getUid());
                healthhourVo.setCountHumidity(Double.valueOf(df.format(counthumidity / matchHealthList.size())));
                healthhourVo.setCountTemp(Double.valueOf(df.format(counttemp / matchHealthList.size())));
                healthhourVo.setDate(DateUtil.format(matchHealthList.get(0).getDatatime(), "yyyy-MM-dd HH"));
                matchHealthVos.add(healthhourVo);
            }
        }
        return AjaxResult.success(matchHealthVos);
    }

    @PostMapping("/listminute")
    public AjaxResult listminute(@RequestBody MatchHealth matchHealth) {
//        某个小时的实际数据
        String beginTime = matchHealth.getBeginTime()+":00:00";
        Date endtime = DateUtil.offsetHour(DateUtil.parse(beginTime), 1);
        matchHealth.setEndTime(DateUtil.format(endtime, "yyyy-MM-dd HH:mm"));
        List<MatchHealth> matchHealthList = matchHealthService.selectMatchHealthList(matchHealth);
        List<MatchHealthMVo> matchHealthMVos = new ArrayList<>();
        matchHealthList.forEach(matchHealth1 -> {
            MatchHealthMVo matchHealthMVo = new MatchHealthMVo();
            BeanUtils.copyProperties(matchHealth1, matchHealthMVo);
            matchHealthMVos.add(matchHealthMVo);
        });
        return AjaxResult.success(matchHealthMVos);
    }


    /**
     * 导出【请填写功能名称】列表
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MatchHealth matchHealth) {
        List<MatchHealth> list = matchHealthService.selectMatchHealthList(matchHealth);
        ExcelUtil<MatchHealth> util = new ExcelUtil<MatchHealth>(MatchHealth.class);
        return util.exportExcel(list, "health");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(matchHealthService.selectMatchHealthById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MatchHealth matchHealth) {
        return toAjax(matchHealthService.insertMatchHealth(matchHealth));
    }

    /**
     * 修改【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MatchHealth matchHealth) {
        return toAjax(matchHealthService.updateMatchHealth(matchHealth));
    }

    /**
     * 删除【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(matchHealthService.deleteMatchHealthByIds(ids));
    }
}
